package lingnan.cn.edu.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lingnan.cn.edu.BaseAndImpl.ItemService;
import lingnan.cn.edu.User.Orderinf;
import lingnan.cn.edu.util.DbAccess;

public class ItemServiceImpl  implements ItemService{

	@Override
	public List<Orderinf> query() {
		List<Orderinf> Orderinfs = new ArrayList<Orderinf>();
		Connection conn = DbAccess.getConnection();
		String SQL = "select * from orderinf";
		
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			
		    prep = conn.prepareStatement(SQL);
			rs = prep.executeQuery();
		    while(rs.next()) {
		    Orderinf orderinf = new Orderinf();
		    orderinf.setOrderId(rs.getInt("orderId"));
		    orderinf.setName(rs.getString("Name"));
		    orderinf.setCount(rs.getInt("Count"));
		    orderinf.setItemPrice(rs.getInt("ItemPrice"));
		   
		    Orderinfs.add(orderinf);
		}
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		return Orderinfs;
	}

	@Override
	public List<Orderinf> findOrder(int orderId) {
		List<Orderinf> orderinfs = new ArrayList<Orderinf>();
		Connection conn = DbAccess.getConnection();
		String SQL = "select * from orderinf where orderid = ?";		
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			prep = conn.prepareStatement(SQL);
			prep.setInt(1, orderId);
			rs = prep.executeQuery();
			while(rs.next()) {
				Orderinf orderinf = new Orderinf();
				orderinf.setOrderId(rs.getInt("orderId"));
				orderinf.setName(rs.getString("Name"));
				orderinf.setCount(rs.getInt("Count"));
				orderinf.setItemPrice(rs.getInt("itemPrice"));
				orderinfs.add(orderinf);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		return orderinfs;
	}
}


